package net.googlecode.exigenlab.task5;

import net.googlecode.exigenlab.task5.calculator.Calculator;
import net.googlecode.exigenlab.task5.calculator.SalaryCalculatorFactory;
import net.googlecode.exigenlab.task5.model.Boss;
import net.googlecode.exigenlab.task5.model.Employee;
import net.googlecode.exigenlab.task5.model.Manager;
import net.googlecode.exigenlab.task5.model.Person;
import net.googlecode.exigenlab.task5.sort.Sorter;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

/**
 * Main application for calculating salary for person
 */
public class SalaryApp {
    private static final BigDecimal BOSS_TAX = new BigDecimal(500);
    private static final BigDecimal MANAGER_TAX = new BigDecimal(250);
    private static final BigDecimal EMPLOYEE_TAX = new BigDecimal(125);

    public static void main(String[] args) {
        Sorter sort = new Sorter();

        Manager boss = createBoss();
        Manager manager = createManager();
        Manager manager2 = createManager2();
        Manager manager3 = createManager3();
        Manager employee = createEmployee();
        Manager employee2 = createEmployee2();
        Manager employee3 = createEmployee3();

        if (args.length > 0) {
            if (args[0].equals("code")) {
                Collection<Manager> testCollection = sort.sortById(createCollection(boss, manager, employee, manager2, manager3, employee2, employee3));
               System.out.println(testCollection);
            } else {
                if (args[0].equals("name")) {
                    Collection<Manager> testCollection = sort.sortByName(createCollection(boss, manager, employee, manager2, manager3, employee2, employee3));
                    System.out.println(testCollection);
                } else {
                    if (args[0].equals("salary")) {
                        Collection<Manager> testCollection = sort.sortBySalary(createCollection(boss, manager, employee, manager2, manager3, employee2, employee3));
                        System.out.println(testCollection);
                    } else {
                        System.out.println("Wrong parameter.");
                    }
                }
            }
        } else {
            for (Manager x : createCollection(boss, manager, employee, manager2, manager3, employee2, employee3)) {
                System.out.println(x);
            }
        }
    }

    private static Collection<Manager> createCollection(Manager... employees){
        Collection<Manager> workers = new ArrayList<Manager>();

        for (Manager x : employees) {
            boolean unic = true;

            Iterator<Manager> it = workers.iterator();

            while (it.hasNext()) {
                if (it.next().getPersonalID() == x.getPersonalID()) {
                    unic = false;
                }
            }

            if (unic) {
                workers.add(x);
            }
        }
        return workers;
    }

    private static BigDecimal calculateSalary(Manager person, BigDecimal tax) {
        Calculator salaryCalculator = SalaryCalculatorFactory.getCalculator(person);
        salaryCalculator.setTax(tax);
        return salaryCalculator.calculateSalary(person);
    }

    public static Manager createManager() {
        Manager manager = new Manager();
        manager.setFirstName("Ivan");
        manager.setLastName("Ivanov");
        manager.setAge(30);
        manager.setPerHour(true);
        manager.setBonus(false);
        manager.setRate(new BigDecimal(25));
        manager.setPersonalID("1234");
        manager.setSalary(calculateSalary(manager, MANAGER_TAX));

        return manager;
    }

    public static Boss createBoss() {
        Boss boss = new Boss();
        boss.setFirstName("Petr");
        boss.setLastName("Petrov");
        boss.setAge(35);
        boss.setBonusAmount(new BigDecimal(1000));
        boss.setRate(new BigDecimal(50));
        boss.setPersonalID("0234");
        boss.setSalary(calculateSalary(boss, BOSS_TAX));

        return boss;
    }

    public static Employee createEmployee() {
        Employee employee = new Employee();
        employee.setFirstName("Semen");
        employee.setLastName("Semenov");
        employee.setAge(55);
        employee.setBonus(true);
        employee.setBonusAmount(new BigDecimal(1500));
        employee.setPerHour(true);
        employee.setRate(new BigDecimal(8));
        employee.setPersonalID("3567");
        employee.setSalary(calculateSalary(employee, EMPLOYEE_TAX));

        return employee;
    }

    public static Employee createEmployee2() {
        Employee employee = new Employee();
        employee.setFirstName("Gennadij");
        employee.setLastName("Gelikh");
        employee.setAge(55);
        employee.setBonus(false);
        employee.setPerHour(true);
        employee.setRate(new BigDecimal(12));
        employee.setPersonalID("3500");
        employee.setSalary(calculateSalary(employee, EMPLOYEE_TAX));

        return employee;
    }

    public static Employee createEmployee3() {
        Employee employee = new Employee();
        employee.setFirstName("Stanislav");
        employee.setLastName("Girnov");
        employee.setAge(55);
        employee.setBonus(true);
        employee.setBonusAmount(new BigDecimal(1500));
        employee.setPerHour(true);
        employee.setRate(new BigDecimal(8));
        employee.setPersonalID("3007");
        employee.setSalary(calculateSalary(employee, EMPLOYEE_TAX));

        return employee;
    }

    public static Manager createManager2() {
        Manager manager = new Manager();
        manager.setFirstName("Sergey");
        manager.setLastName("Petrov");
        manager.setAge(30);
        manager.setPerHour(true);
        manager.setBonus(false);
        manager.setRate(new BigDecimal(35));
        manager.setPersonalID("0034");
        manager.setSalary(calculateSalary(manager, MANAGER_TAX));

        return manager;
    }

    public static Manager createManager3() {
        Manager manager = new Manager();
        manager.setFirstName("Alexandr");
        manager.setLastName("Ivanov");
        manager.setAge(30);
        manager.setPerHour(true);
        manager.setBonus(true);
        manager.setBonusAmount(new BigDecimal(500));
        manager.setRate(new BigDecimal(20));
        manager.setPersonalID("1200");
        manager.setSalary(calculateSalary(manager, MANAGER_TAX));

        return manager;
    }
}
